home *** CD-ROM | disk | FTP | other *** search
- ; rcmerge
- ; merge vector entities into raster by colour with differing brushwidth
- ;
- ; Derrick Oswald
- ; Nexsys Consulting Inc.
- ; 44 Douglas Drive
- ; Ayr, Ontario
- ; N0B 1E0
- ; (519) 632-8243
- ; (519) 632-8244 FAX
- ;
- (setq ColourFile "COLOUR.MAP")
-
- ; group
- ; gets the group value given a group and an associative list
- ; parameters:
- ; a - group code, usually INT
- ; b - associative list, LIST
- ; returns:
- ; value, could be any type
- (defun group ( a b )
- (cdr (assoc a b))) ; take tail of dotted associative list element
-
- ; grpget
- ; gets the group value given a group and an entity name
- ; parameters:
- ; a - group code, usually INT
- ; b - entity name, ENAME
- ; returns:
- ; value, could be any type
- (defun grpget ( a b )
- (group a (entget b))) ; pass associative list onto group
-
- ; makelayercolourlist
- ; scan the layer table and make an associative list of (layer . colour)
- (defun makelayercolourlist ( / l first)
-
- ; set up
- (setq l ())
- (setq first T)
-
- ; scan table
- (while (setq layer (tblnext "layer" first))
-
- ; add layer name and colour to l
- (setq l (cons (cons (group 2 layer) (abs (group 62 layer))) l))
-
- ; prepare for subsequent call
- (setq first ()))
-
- ; return the list reversed to original order
- (reverse l))
-
- ; y-or-n-p
- ; a predicate that gets a yes or no answer similar to AutoCAD
- ; parameters:
- ; st - prompt string, the default is tacked on it in angle brackets
- ; by this function, STR
- ; def - the default response,
- ; non-nil - default answer to question is Yes
- ; nil - default answer to question is No
- ;
- ; NOTE: example useage is to confirm a deletion
- ; (if (y-or-n-p "Delete it?" ())
- ; (command "erase" (entlast)))
- (defun y-or-n-p ( string defaul / answer )
- (initget 0 "Yes No")
- (setq answer (getkword (strcat string " <" (if defaul "Y" "N") "> ")))
- (cond
- ((null answer) defaul)
- ((= answer "Yes"))
- ((= answer "No") ())))
-
- ; intget
- ; to get an integer with a default
- ; parameters:
- ; p - prompt string or NIL, STR
- ; d - default value, INT
- (defun intget ( p d / s)
- (if ; did user hit enter or space
- (null
- (setq s
- (getint
- (strcat "\n"
- (if (= (type p) 'STR) p "Integer") ; if there is a prompt use it
- " <" ; display default value
- (if (numberp d)
- (itoa (fix d))
- "")
- ">: "))))
- d
- s))
-
- ; strget
- ; get a string with a default
- ; parameters:
- ; p - prompt string or NIL, STR
- ; d - default value, REAL
- ; r - indicates if string can contain spaces or not, T or NIL
- ; T = confirm with <CR>
- ; NIL = up to first <SPACE> OR <CR>
- (defun strget(p d r / s)
- (if
- (zerop
- (strlen
- (setq s
- (getstring
- r
- (strcat "\n"
- (if (= (type p) 'STR) p "String") ; if there is a prompt use it
- " <" ; display default value
- (if (= (type d) 'STR) d "")
- ">: ")))))
- d
- s))
-
- ; list of names for colours
- (setq colournames (list
- '(1 . "RED")
- '(2 . "YELLOW")
- '(3 . "GREEN")
- '(4 . "BLUE")
- '(5 . "CYAN")
- '(6 . "MAGENTA")
- '(7 . "WHITE")))
-
- ; colourname
- ; return the name of the colour
- (defun colourname (n / s)
-
- ; look it up
- (setq s (assoc n colournames))
-
- ; cover un-named colours
- (if s
- (setq s (cdr s))
- (setq s ""))
-
- ; return colour
- s)
-
- ; savecolours
- ; save the colour list to a file
- (defun savecolours (l / filename f n)
- (setq filename (strget "File name for saving colour-brushwidth list"
- (if FileName FileName ColourFile) ()))
- ; open the file
- (if (setq f (open filename "w"))
- (progn
- ; echo name
- (princ (strcat "\nSaving file \"" filename "\""))
- ; write each colour
- (foreach n l
- (princ (strcat (itoa (car n)) " " (itoa (cdr n)) "\n") f))
- (close f))
- (princ (strcat "\nCan't open file \"" filename "\"")))
- (princ))
-
- ; pos
- ; searches for a string in another string
- ; parameters:
- ; a - source string, STR
- ; b - test string, STR
- ; c - direction flag, 1 for forward, -1 for backwards, INT
- ; returns:
- ; the position of the character in the string if found
- ; NIL if not found
- (defun pos ( a b c / d e f g )
- (setq d (strlen a) ; size of source and done flag
- e (strlen b)) ; size of test
- (if (= c 1) ; if forward:
- (setq f 1 ; start
- g (1+ d)) ; limit
- (setq f d ; start
- g 0)) ; limit
- (while (and d (/= f g)) ; not found and not end of source
- (if (= b (substr a f e)) ; if test string is at our current position
- (setq d nil) ; reset flag
- (setq f (+ f c)))) ; else bump pointer
- (if (not d) ; return position or NIL
- f))
-
- ; parse
- ; parse the next word from the string
- ; parameters:
- ; qstring - a quoted string variable, (QUOTE STR)
- ; delim - delimiter character, STR
- ; returns:
- ; the parsed word or NIL if string is empty, STR
- ; NOTE: modifies the original string by removing the parsed word and delimiter
- ; qstring, delim, string, count & len must be unique to avoid global conflict
- (defun parse ( qstring delim / string count len )
- (setq string (eval qstring)) ; get a copy of string
- (cond
- ((or (null string) (= string ""))
- (set qstring NIL)) ; if no characters left return NIL
- ((or (null delim) (= delim "")) "") ; if nothing asked for return nothing
- (T
- (setq len (strlen delim))
- (while (and (setq count (pos string delim 1)) (= count 1))
- (setq string (substr string (1+ len)))) ; pass leading delimiters
- (set qstring (if count (substr string (+ count len)))) ; rest or nil
- (if count (substr string 1 (1- count)) string)))) ; return part or whole
-
- ; restorecolours
- ; restore the colour list from a file
- (defun restorecolours ( / l f a colour width)
- ; get the file name from the user
- (setq FileName (strget "File name to retrieve" ColourFile ()))
- ; scan for it
- (setq FileName (findfile FileName))
- ; open the file
- (if (setq f (open FileName "r"))
- (progn
- ; echo name
- (princ (strcat "\nReading file \"" FileName "\""))
- ; read each colour
- (while (setq a (read-line f))
- (setq colour (parse 'a " "))
- (setq width (parse 'a " "))
- (setq l (cons (cons (atoi colour) (atoi width)) l)))
- (close f))
- (princ (strcat "\nCan't open file \"" FileName "\"")))
- ; return the list in original order
- (reverse l))
-
- ; makecolourbrushwidthlist
- ; interactively request brushwidth for colour from the user
- (defun makecolourbrushwidthlist (/ filename l i colour n)
-
- ; move to text screen
- (textscr)
-
- (while (null l)
- ; get the existing list or make a new one
- (if colourbrushwidthlist
- (setq l colourbrushwidthlist)
- (progn
- ; ask if we should try to read it from file
- (if (y-or-n-p "\nRetrieve colour-brushwidth list from file?" ())
- (setq l (restorecolours))
- ; else have to make it
- (progn
-
- ; get default brushwidth
- (setq brushwidth (intget "Default brushwidth" 3))
-
- ; make the list
- (setq i 0)
- (setq l ())
- (while (<= i 7)
- (setq l (cons (cons i brushwidth) l))
- (setq i (1+ i)))
- (setq l (reverse l)))))))
-
- ; set up
- (setq colour -1)
-
- ; repeat until the user is happy
- (while (not (zerop colour))
-
- ; increment colour for next round
- (setq colour (1+ colour))
-
- ; print out the colour list
- (princ "\n")
- (foreach n l
- (if (not (zerop (car n)))
- (princ (strcat "Colour " (itoa (car n)) " " (colourname (car n)) " width " (itoa (cdr n)) "\n"))))
-
- ; ask for colour
- (setq colour (intget "Colour number (0 to exit)" colour))
-
- ; if it's not in our list add it
- (if (not (assoc colour l))
- (setq l (reverse (cons (cons colour brushwidth) (reverse l)))))
-
- ; if it's not zero get brushwidth
- (if (not (zerop colour))
- (progn
- (setq brushwidth (intget "Brushwidth" (group colour l)))
- ; replace it in the list
- (setq l (subst (cons colour brushwidth) (assoc colour l) l)))))
-
- ; ask if user wants to save the file
- (if (y-or-n-p "\nSave colour-brushwidth list to disk file?" T)
- (savecolours l))
-
- ; back to graph screen
- (graphscr)
-
- ; return the list
- l)
-
- ; c:rcmerge
- ; main routine to merge coloured objects
- (defun c:rcmerge ()
-
- ; get a layer list
- (setq layercolours (makelayercolourlist))
-
- ; get the image to be merged into
- (if (setq block (rimage))
- (progn
-
- ; get the objects to be merged
- (princ "\nIdentify objects to be merged: ")
- (if (and (setq ss (ssget)) (> (sslength ss) 0))
- (progn
-
- ; get the colour list
- (setq colourbrushwidthlist (makecolourbrushwidthlist))
-
- ; freah line
- (princ "\n")
-
- ; repeat for every colour or until no objects are left to merge
- (setq colour 1)
- (while (and (< colour 255) (> (sslength ss) 0))
-
- ; echo progress
- (princ (strcat "\rColour " (itoa colour)))
-
- ; start with an empty selection set of objects with that colour
- (setq cset (ssadd))
-
- ; for every object in the selection set of objects
- (setq index 0)
- (while (< index (sslength ss))
-
- ; get the entity name...
- (setq e (ssname ss index))
-
- ; ...and a list of it's data
- (setq l (entget e))
-
- ; test explicit colour
- (if (assoc 62 l)
-
- ; if it's colour is explicitly this colour add it to our list
- (if (= colour (group 62 l))
- (ssadd e cset))
-
- ; or if it's layer is this colour add it to the list
- (if (= colour (group (group 8 l) layercolours))
- (ssadd e cset)))
-
- ; increment index and repeat
- (setq index (1+ index)))
-
- ; if there's objects of this colour
- (if (> (sslength cset) 0)
-
- ; merge them in
- (progn
-
- ; must delete them from further consideration
- (setq index 0)
- (while (< index (sslength cset))
- (ssdel (ssname cset index) ss)
- (setq index (1+ index)))
-
- ; get the brushwidth or default
- (if (assoc colour colourbrushwidthlist)
- (setq brushwidth (group colour colourbrushwidthlist))
- (setq brushwidth (group 0 colourbrushwidthlist)))
-
- ; echo the progress
- (princ (strcat " has " (itoa (sslength cset)) " entities"))
- (princ (strcat " merging with width " (itoa brushwidth) "\n"))
-
- ; merge the entities
- (rmerge block cset brushwidth)))
-
- ; increment colour and repeat
- (setq colour (1+ colour)))
-
- ; end of (if (ssget)
- )
- (princ "\nNo objects selected"))
-
- ; end of (if (entsel)
- )
- (princ "\nNo raster block picked"))
-
- (princ))
-